package com.imooc.service.impl.center;

import com.github.tobato.fastdfs.domain.fdfs.StorePath;
import com.github.tobato.fastdfs.service.FastFileStorageClient;
import com.imooc.service.center.FdfsService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;


@Service
public class FdfsServiceImpl implements FdfsService {

    @Autowired
    private FastFileStorageClient fastFileStorageClient;


    @Override
    public String upload(MultipartFile file, String fileExtName) throws Exception {

        StorePath storePath = fastFileStorageClient.uploadFile(file.getInputStream(),
                                file.getSize(),
                                fileExtName,
                                null);

        String path = storePath.getFullPath();

        return path;
    }

    //@Override
    //public String uploadOSS(MultipartFile file, String userId, String fileExtName) throws Exception {
    //
    //    // 构建ossClient
    //    OSS ossClient = new OSSClientBuilder()
    //            .build(fileResource.getEndpoint(),
    //                    fileResource.getAccessKeyId(),
    //                    fileResource.getAccessKeySecret());
    //
    //    InputStream inputStream = file.getInputStream();
    //
    //    String myObjectName = fileResource.getObjectName() + "/" + userId + "/" + userId + "." + fileExtName;
    //
    //    ossClient.putObject(fileResource.getBucketName(), myObjectName, inputStream);
    //    ossClient.shutdown();
    //
    //    return myObjectName;
    //}
}
